<?php
namespace Home\Model;
use Think\Model;

class UserModel extends Model {


	protected $_validate = array(
		// array('username', 'require', '帐号不能为空', 1, 'regex', 3),
		array('username', 'checkAccount', '帐号已经存在', 1, 'callback', 3),
		array('password', 'require', '密码不能为空', 1, 'regex', 4),
	);

	protected $_auto = array(
		array('password', 'md5pass', 3, 'callback'),
		array('createtime', 'time', 1, 'function'),		

	);
	

	/**
	 * 检查帐号是否存在
	 * @return [type] [description]
	 */
	protected function checkAccount() {
		$account = I('post.username');

		$map['username'] = $account;
		if(I('post.uid')){
			$map['u.uid'] = array('neq',I('post.uid'));
		}			

		$res = $this->alias('u')->where($map)->getField('u.uid');
		if($res) {
			return false;
		} else {
			return true; 
		}
	}

	protected function md5pass(){
		$password=I('post.password');
		if(!$password){
			return false;
		}else{
			return md5(C('PWD_SALT').$password);
		}
	}

	
}
